package psn.zjk.exporter.sample;

import org.apache.poi.xssf.usermodel.XSSFCell;
import psn.zjk.exporter.*;

import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 *
 * @author zjk
 * @CREATE_TIME 2020/11/14 15:54
 */
public class ExporterSample {

    public static void main(String[] args) throws IOException {
        List<String> headerFileds = new ArrayList<>();
//        headerFileds.add("name");
        headerFileds.add("sex");
        headerFileds.add("birthday");
        //固定列头的表格
        ExcelDataBuilder excelDataBuilder2 = ExcelDataBuilder.newBuilder(headerFileds, "name");
        //固定行头的表格
        ExcelDataBuilder excelDataBuilder = ExcelDataBuilder.newBuilder("name", headerFileds);
        //动态表格
        ExcelDataBuilder.newBuilder("行头属性","列头属性","展示属性");

        //载入数据
        List<User> datas = getUsers();
        excelDataBuilder.setDatas(datas);
        //插入单独的行
//        excelDataBuilder.insertRow()
        //组装数据，用于填充
        ExcelData excelData = excelDataBuilder.build();

        //载入导出模板
//        ExcelSheetBuilder excelSheetBuilder = ExcelSheetBuilder.newBuilder("path","file");
        //无模板导出
        ExcelSheetBuilder excelSheetBuilder = ExcelSheetBuilder.newBuilder();
        excelSheetBuilder.setData(excelData);
        //样式生成器
        CellStyleBuilder cellStyleBuilder = excelSheetBuilder.newCellStyleBuilder();
        //列宽自适应
        excelSheetBuilder.autoSizeColumn(1);
        excelSheetBuilder.setCellListener(new CellListener() {
            @Override public void beforeSet(SheetCell sheetCell) {
                String header = sheetCell.getSheetColumn().getHeader();
                if (header!=null&&header.equals("sex")){
                    if (sheetCell.getValue()!=null&&sheetCell.getValue().equals(1)){
                        sheetCell.setValue("男");
                    }
                }
            }

            @Override public void afterSet(XSSFCell cell) {
            }
        });

        //导出
        excelSheetBuilder.write("out.xlsx");


    }

    private static List<User> getUsers(){
        ArrayList<User> users = new ArrayList<>();
        LocalDate date = LocalDate.of(1990, 01, 01);
        Random random = new Random();
        users.add(new User("zhansan",1,new Date(date.toEpochDay())));
        users.add(new User("lisi",2,new Date(date.plusDays(random.nextInt(1024)).toEpochDay())));
        users.add(new User("wanwu",1,new Date(date.plusDays(random.nextInt(1024)).toEpochDay())));
        users.add(new User("66",11,new Date(date.plusDays(random.nextInt(1024)).toEpochDay())));
        users.add(new User("dada",3,new Date(date.plusDays(random.nextInt(1024)).toEpochDay())));
        return users;
    }
}